// pages/me//pages/portrait/portrait.js
const {
  http
} = require("../../../utils/apiutils")
import { input } from '../../../utils/tools'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    labelList: [],
    describe: '',
    form: {
      id: '',
      userId: 0,
      professional: 0,
      grasp: 0,
      opretion: 0,
      other: 0,
      expression: 0,
    },
    labelArr: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ['form.userId']: options.userId - 0
    })
    this.userPortrait({ userId: this.data.form.userId })
    this.queryTreeByType({ type: 7 })
    this.getDescribe({ id: this.data.form.userId })
    this.getActiveLabel({ userId: this.data.form.userId })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  input(event) {
    input.bind(this)(event)
  },
  // 查看用户画像
  userPortrait(params) {
    wx.showLoading()
    http.userPortrait(params, res => {
      wx.hideLoading()
      if (res.status == 200) {
        if (res.obj.myScore) {
          this.setData({
            ['form.id']: res.obj.myScore.id ? res.obj.myScore.id : '',
            ['form.professional']: res.obj.myScore.professional ? res.obj.myScore.professional : 0,
            ['form.grasp']: res.obj.myScore.grasp ? res.obj.myScore.grasp : 0,
            ['form.opretion']: res.obj.myScore.opretion ? res.obj.myScore.opretion : 0,
            ['form.other']: res.obj.myScore.other ? res.obj.myScore.other : 0,
            ['form.expression']: res.obj.myScore.expression ? res.obj.myScore.expression : 0,
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, err => { wx.hideLoading() })
  },
  // 获取描述
  getDescribe(params) {
    http.getDescribe(params, res => {
      if (res.status == 200) {
        this.setData({
          describe: res.obj.profile
        })
      }
    }, err => { })
  },
  // 获取标签
  getActiveLabel(params) {
    http.getActiveLabel(params, res => {
      res.obj.map(item => {
        this.data.labelList.map(ite => {
          ite.children.map(it => {
            if (it.id == item) {
              it.checked = true
              ite.open = true
            }
          })
        })
      })
      setTimeout(() => {
        this.setData({
          labelList: this.data.labelList
        })
      }, 1000)
    }, err => { })
  },
  // 获取标签树结构
  queryTreeByType(params) {
    http.queryTreeByType(params, res => {
      if (res.status == 200) {
        res.obj.map(item => {
          item.open = false
          item.children.map(ite => {
            ite.checked = false
          })
        })
        this.setData({
          labelList: res.obj
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, err => { })
  },
  // 点击行
  onClickRow(e) {
    let index = e.currentTarget.dataset.num
    this.data.labelList[index].open = !this.data.labelList[index].open
    this.setData({
      labelList: this.data.labelList
    })
  },
  // 点击标签
  onSelectLabel(e) {
    let pi = e.currentTarget.dataset.key
    let i = e.currentTarget.dataset.num
    let info = e.currentTarget.dataset.info
    this.data.labelList[pi].children[i].checked = !this.data.labelList[pi].children[i].checked
    this.setData({
      labelList: this.data.labelList
    })
  },
  // 点击保存
  clickSubmit() {
    this.getLabelId() //先获取标签id
    this.saveAbility()
    this.saveLabel() //在保存标签
    this.saveDescribe()
  },
  // 获取选中标签id
  getLabelId() {
    this.data.labelList.map(item => {
      item.children.map(ite => {
        if (ite.checked) {
          this.data.labelArr.push({ labelId: ite.id, labelName: ite.name })
        }
      })
    })
  },
  // 保存能力值
  saveAbility() {
    http.saveAbility(this.data.form, res => {
      if (res.status != 200) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, err => { })
  },
  // 保存标签
  saveLabel() {
    let params = {
      userId: this.data.form.userId,
      list: this.data.labelArr
    }
    wx.showLoading()
    http.saveLabel(params, res => {
      wx.hideLoading()
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })
    }, erro => { wx.hideLoading() })
  },
  // 保存描述
  saveDescribe() {
    let params = {
      id: this.data.form.userId,
      profile: this.data.describe,
    }
    http.saveDescribe(params, res => {
      if (res.status == 200) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        wx.navigateBack({
          delta: 1,
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, err => { })
  }

})